package com.androturka;

import java.util.ArrayList;

import com.androturka.IDataAdapter.EarthQuake;
import com.androturka.IDataAdapter.IArrayListDataListener;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.provider.Settings.Secure;
import android.util.Log;


public class ClientService extends Service {

	private LocationManager locationManager;
	private String bestProvider;
	public Location location;
	

	private EarthQuake[] earthquakes;
	private int eartquakeCount;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		//Log.d(TAG, "onCreate");
	}

	@Override
	public void onDestroy() {
		//Log.d(TAG, "onDestroy");
	}
	
	@Override
	public void onStart(Intent intent, int startid) {
		//Log.d(TAG, "onStart");

		//C2DMessaging.register(this, "depremgunlugu@gmail.com");
		//super.onCreate(savedInstanceState);

		final EarthQuakeNotification eqNotification = new EarthQuakeNotification( getApplicationContext());
		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

		Criteria criteria = new Criteria();
		bestProvider = locationManager.getBestProvider(criteria, false);		
		
		location = locationManager.getLastKnownLocation(bestProvider);		
		/*
		if ( location != null )
			Toast.makeText(Main.this, "Konum Bilgisi Başarıyla Alındı",Toast.LENGTH_SHORT).show();
		else
			Toast.makeText(Main.this, "Konum Bilgisi Alınamadı",Toast.LENGTH_SHORT).show();
		*/

		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {

			IDataAdapter adapter = new WebAdapter();

			//adapter.getEarthQuakeList("getEarthQuakes","count",Integer.toString(eqCount), new IArrayListDataListener() {
			//adapter.getEarthQuakeList("getAfterEarthQuakes","afterdate","20120420", new IArrayListDataListener() {
			String deviceId = Secure.getString(getApplicationContext().getContentResolver(),
					Secure.ANDROID_ID);
			adapter.getEarthQuakeList("getMyEarthQuakes","deviceid",deviceId, new IArrayListDataListener() {
				@Override
				public void dataArrived(ArrayList<Object> data) {
					if (data != null) {
						eartquakeCount = data.size();
						
						if (eartquakeCount > 0) {
							earthquakes = new EarthQuake[eartquakeCount];
							for (int i = 0; i < eartquakeCount; i++) {
								earthquakes[i] = (EarthQuake) data.get(i);
								if ( location != null )
									earthquakes[i].distance = distFrom(location.getLatitude(),location.getLongitude(),earthquakes[i].latitude,earthquakes[i].longitude);
								else
									earthquakes[i].distance = (double) -1;
								
								Log.d("test", earthquakes[i].toString());
								eqNotification.displayNotification(earthquakes[i]);

							}
							new EarthQuakeAdapter(
									getApplicationContext(), earthquakes );

						}
					}
				}
			});
		}	
	}

	public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
		double earthRadius = 3958.75;
		double dLat = Math.toRadians(lat2-lat1);
		double dLng = Math.toRadians(lng2-lng1);
		double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
		           Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
		           Math.sin(dLng/2) * Math.sin(dLng/2);
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
		double dist = earthRadius * c;
		
		return dist;
    }
}